package com.icss.ah.vehicleInfo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.j2ee.util.UUID;

@Service
@Transactional
public class VehicleInfoService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	public List queryVehicleList(HashMap map) {
		List list = new ArrayList();
		try{
			//根据登录账号获取账号基本信息
			list = sqlSessionTemplate.selectList("VehicleInfoBean.queryVehicleList", map);
			infolog.info("VehicleInfoService|queryVehicleList|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("VehicleInfoService|queryVehicleList", e);
		}
		return list;
		
	}
	
	public Long queryVehicleCount(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("VehicleInfoBean.queryVehicleCount", map);
			infolog.info("VehicleInfoService|queryVehicleCount|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleInfoService|queryVehicleCount", e);
		} 
		return reValue;
	}
	
	public HashMap queryVehicleMap(HashMap map) {
		HashMap vehicleMap=new HashMap();
		try{
			vehicleMap = sqlSessionTemplate.selectOne("VehicleInfoBean.queryVehicleMap", map);
			infolog.info("VehicleInfoService|queryVehicleMap|vehicleMap:"+vehicleMap);
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("VehicleInfoService|queryVehicleMap", ex);
		} 
		return vehicleMap == null ? new HashMap() : vehicleMap;
	}
	
	
	public boolean saveVehicle(HashMap map,String carUuid,String operat){
		boolean reValue = false;
		try {
			if(carUuid.equals("")||carUuid==null){
				map.put("carUuid",new UUID().toString() );
			}else{
				map.put("carUuid",carUuid);
			}
			if(operat.equals("add")){
				sqlSessionTemplate.insert("VehicleInfoBean.insertVehicle", map);
			}else if(operat.equals("edit")){
				sqlSessionTemplate.update("VehicleInfoBean.updateVehicle", map);
			}
			reValue = true;
			infolog.info("VehicleInfoService|saveVehicle|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleInfoService|saveVehicle", e);
		} 
		return reValue;
	}
	
	public boolean deleteVehicle(HashMap map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.delete("VehicleInfoBean.deleteVehicle", map);
			infolog.info("VehicleInfoService|deleteVehicle|f:"+f);
			reValue = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleInfoService|deleteVehicle", e);
		} 
		return reValue;
	}
	
	public boolean dealStatus(HashMap map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.update("VehicleInfoBean.dealStatus", map);
			infolog.info("VehicleInfoService|dealStatus|f:"+f);
			reValue = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleInfoService|dealStatus", e);
		} 
		return reValue;
	}
	/**
	 * 验证车牌号是否存在
	 * @param map
	 * @return
	 */
	public boolean checkCarNoExist(HashMap map) {
		boolean reValue = false;
		try {
			Integer count = sqlSessionTemplate.selectOne("VehicleInfoBean.checkCarNoExist", map);
			if(count==0){
				reValue = true;
			}
			infolog.info("VehicleInfoService|checkCarNoExist|count:"+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleInfoService|checkCarNoExist", e);
		} 
		return reValue;
	}
}
